/* Copyright 2001,2002,2003 NAH6 BV
 * All Rights Reserved
 *
 *  $Header: /var/lib/cvs/secphone/ui/userdata/UDCPinger.h,v 1.5 2003/11/21 12:39:24 itsme Exp $
 *
 *
 */


#ifndef __PINGER_H__

#include "userdatahandler.h"

class CCPhoneDlg;

typedef struct _tagPingMessage {
    BYTE msg;
    BYTE pingid;
    BYTE pongid;
    BYTE idnybles;
    LONG size;
    DWORD time_pingsent;
    DWORD time_pongsent;
    BYTE payload[1];
} PingMessage;

class UDCPinger : public UDClientHandler {
public:
    UDCPinger(CCPhoneDlg& dlg);
    virtual ~UDCPinger();

    void Start(long interval, long size);

    virtual bool StopSending();

    void SendPings(int nCount, int nInterval, int nSize);
    virtual bool HandleUserdataMessage(const ByteVector& data);


private:
    CCPhoneDlg& m_dlg;

    long m_interval;    // in milliseconds
    long m_size;        // in bytes
    DWORD m_timerid;
    long m_nr_pings_to_send;

    int m_pingid;       // current ping id
    int m_pongid;

    DWORD m_pingsSent;
    DWORD m_pongsSent;

    DWORD m_pingsReceived;
    DWORD m_pongsReceived;
    DWORD m_bytesReceived;
    DWORD m_roundtriptime;

    void reset();

    bool HandlePingMessage(const ByteVector& msg);
    bool HandlePongMessage(const ByteVector& msg);

    bool SendPongMessage(int size, int pingid, DWORD time_pingsent);
    bool SendPingMessage();
    
    static void CALLBACK staticTimerProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime);
    void TimerProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime);

    void UpdateDialog();
};


#define __PINGER_H__
#endif
